package com.ag.work2.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ag.bean.TbEvent;
import com.ag.bean.TbEventPlanStart;
import com.ag.bean.TbEventPlanStartId;
import com.ag.bean.TbPlanStandard;
import com.ag.bean.TbPowerTransfer;
import com.ag.bean.TbUser;
import com.ag.common.IDUtil;
import com.ag.lzm.dao.BaseDAO;
import com.ag.lzm.dao.TbEventDAO;
import com.ag.lzm.dao.TbPlanStandardDAO;
import com.ag.work2.dao.TbEventPlanStartDAO;
import com.ag.work2.dao.TbPowerTransferDAO;
import com.ag.work2.dao.TbProposeDAO;
import com.ag.work2.dao.TbProposeRecordDAO;
import com.ag.work2.service.IPlanStartStandardService;

@Service("LZMPlanStartStandardServiceImpl")
public class PlanStartStandardServiceImpl implements IPlanStartStandardService {
	@Resource(name = "LZMTbPlanStandardDAO")
	private TbPlanStandardDAO planStandardDAO;
	@Resource(name = "LZMBaseDAO")
	private BaseDAO baseDAO;
	@Resource(name = "LZMTbEventDAO")
	private TbEventDAO eventDAO;
	@Resource(name = "LZMTbEventPlanStartDAO")
	private TbEventPlanStartDAO eventPlanStartDAO;
	@Resource(name = "LZMTbPowerTransferDAO")
	private TbPowerTransferDAO powerTransferDAO;
	@Resource(name = "LZMTbProposeDAO")
	private TbProposeDAO proposeDAO;
	@Resource(name = "LZMTbProposeRecordDAO")
	private TbProposeRecordDAO proposeRecordDAO;

	@Override
	public List<TbPlanStandard> findPlanStartStandard(int planLevel) {
		String hql = "from TbPlanStandard psd where psd.PLevel = "+planLevel+" and psd.a ="+ IDUtil.PRESERVE;
		return planStandardDAO.findByPlanLevel(hql);
	}

	@Override
	public void savePlanStartStandard(String elIds, String EId) {
		String queryString = "select count(*) from TbEventPlanStart eps where eps.id.EId = ?";
		// Object[] objects = { elId, EId };
		Long rst = (Long) baseDAO.find(queryString, EId).get(0);
		if (rst != 0) {
			// eventPlanStartDAO.delete(eventPlanStartDAO.findById(epsid));
			String tm = "delete from TbEventPlanStart eps where eps.id.EId = '"
					+ EId + "'";
			try {
				baseDAO.operate(tm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		String[] elIdArray=Pattern.compile(",").split(elIds);
		
	for(String elIdstring:elIdArray){
		System.out.println(elIdstring);
		System.out.println(EId+"*******");
		TbEventPlanStartId epsid = new TbEventPlanStartId();
		epsid.setEId(EId);
		epsid.setElId(elIdstring);
		TbEventPlanStart eps = new TbEventPlanStart();
		eps.setId(epsid);
		eventPlanStartDAO.save(eps);
	 }	
	}

	@Override
	public TbEvent findCurrentEvent(String EId) {
		return eventDAO.findById(EId);
	}

	@Override
	public void savePowerTransfer(TbUser orgUser, TbUser dstUser, String EId) {
		TbPowerTransfer pt = new TbPowerTransfer();
		pt.setTbUserByPfOrg(orgUser);
		pt.setTbUserByPfDst(dstUser);
		pt.setTbEvent(eventDAO.findById(EId));
		pt.setC(getCurrentTime());
		powerTransferDAO.save(pt);
		//处理呈批
		String queryString = "update TbPropose p set p.a = '"+dstUser.getUId()+"' where p.a = '"+orgUser.getUId()+"' and p.tbEvent.EId = '"+EId+"'";
		try {
			baseDAO.operate(queryString);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		String queryString1 = "update TbProposeRecord pr set p.a = '"+dstUser.getUId()+"' where p.a = '"+orgUser.getUId()+"'";
//		try {
//			baseDAO.operate(queryString1);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		// Object[] objects = { elId, EId };
		//Long rst = (Long) baseDAO.find(queryString, EId).get(0);
	}

	private String getCurrentTime() {
		SimpleDateFormat localTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return localTime.format(new Date());
	}

	@Override
	public List<TbPowerTransfer> findPowerTransfer(TbUser currentUser,
			int firstRow, int maxRow) {
		String queryString = "from TbPowerTransfer pt join fetch pt.tbUserByPfOrg org join fetch pt.tbUserByPfDst "
				+ "dst join fetch pt.tbEvent where org.UId = '"
				+ currentUser.getUId()
				+ "' or dst.UId = '"
				+ currentUser.getUId() + "'";
		// Object[] objects = { currentUser.getUId(), currentUser.getUId() };
		try {
			return baseDAO.find(queryString, firstRow, maxRow);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Long findPowerTransferRC(TbUser currentUser) {
		String queryString = "select count(*) from TbPowerTransfer pt join pt.tbUserByPfOrg org join pt.tbUserByPfDst "
				+ "dst join pt.tbEvent where org.UId = ? or dst.UId = ?";
		Object[] objects = { currentUser.getUId(), currentUser.getUId() };
		return (Long) baseDAO.find(queryString, objects).get(0);
	}

}
